// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import axios from 'axios'
Vue.prototype.$axios = axios
import VueAxios from 'vue-axios'
import Qs from 'qs'
import App from './App'
import router from './router'
import store from './store'
import MuseUI from 'muse-ui'
import 'muse-ui/dist/muse-ui.css'
import Driective from './tool/directive'
Vue.use(Driective)
//import 'muse-ui/dist/theme-light.css'
import {
  Carousel,
  CarouselItem,
  Button,
  Col,
  Container,
  Form,
  FormItem,
  Input,
  Main,
  Row,
  Message,
  Table,
  TableColumn,
  Select,
  Option,
  Dialog,
  Radio,
  RadioGroup,
  MessageBox,
  DatePicker,
  Pagination,
  Collapse,
  Popover,
  CollapseItem,
  Tabs,
  TabPane,
  Upload,
  Card,
  Checkbox,
  Loading,
  CheckboxGroup,
  InputNumber,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  TimePicker,
  Menu,
  Submenu,
  MenuItem,
  MenuItemGroup,
  ButtonGroup,

} from 'element-ui';

import MyCard from './components/MyComponents/Card'
import mtips from './components/MyComponents/tips'
import FastClick from 'fastclick'
FastClick.attach(document.body);
Vue.component("mtips", mtips);
Vue.component("MyCard", Card);
Vue.use(MuseUI);
Vue.use(Popover)
Vue.use(Carousel);
Vue.use(CarouselItem);
Vue.use(ButtonGroup);
Vue.use(Button);
Vue.use(Row);
Vue.use(Loading);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Col);
Vue.use(Input);
Vue.use(Container);
Vue.use(Main);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Select);
Vue.use(Option);
Vue.use(Dialog);
Vue.use(Radio);
Vue.use(RadioGroup);
Vue.use(DatePicker);
Vue.use(Pagination);
Vue.use(Collapse);
Vue.use(CollapseItem);
Vue.use(Tabs);
Vue.use(TabPane);
Vue.use(Upload);
Vue.use(Card);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(InputNumber);
Vue.use(Dropdown);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(TimePicker);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);

Vue.prototype.$message = Message;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;

var axios_instance = axios.create({
  transformRequest: [function (data) {
    data = Qs.stringify(data);
    return data;
  }],
});
Vue.use(VueAxios, axios_instance);
Vue.config.productionTip = false;

Vue.prototype.sum = function (value) {
  let result = 0;
  for (let i = 0; i < value.length; i++) {
    if (!value[i]) value[i] = 0;
    result += value[i] * 1;
  }
  return result;
};
//请求
import moment from 'moment'
Vue.filter('moment', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
});
axios.interceptors.request.use(
  config => {
    let token = window.sessionStorage.getItem("DK_token");
    if (token) {
      config.headers.token = `${token}`;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);
//响应
axios.interceptors.response.use(
  res => {
    return res
  },
  err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          Message('请求错误')
          break;
        case 401:
          window.location.href = err.response.data.url
          Message('未授权，请重新登录')
          break;
        case 403:
          Message('拒绝访问')
          break;
        case 404:
          Message('请求错误,未找到该资源')
          break;
        case 405:
          Message('请求方法未允许')
          break;
        case 408:
          Message('请求超时')
          break;
        case 500:
          Message('服务器端出错')
          break;
        case 501:
          Message('网络未实现')
          break;
        case 502:
          Message('网络错误')
          break;
        case 503:
          Message('服务不可用')
          break;
        case 504:
          Message('网络超时')
          break;
        case 505:
          Message('http版本不支持该请求')
          break;
        default:
          Message(`连接错误${err.response.status}`)
      }
    } else {
      Message('连接到服务器失败')
    }
    return Promise.resolve(err.response)
  }
);
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
});
